/**
  ******************************************************************************
  * @file    w32f006_stimer.c
  * @author  WeiLink Technology 
  * @version V0.0.0
  * @date    09/01/2020
  * @brief   This file provides all the STIMER firmware functions.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, WeiLink Technology SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT WeiLink Technology</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "w32f006_stimer.h"
#include "w32f006_rcc.h"

/** @addtogroup W32F006_StdPeriph_Driver
  * @{
  */

/** @defgroup Simple-TIMER
  * @brief Simple-TIMER driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup Simple-TIMER_Private_Functions
  * @{
  */

/**
  * @brief  Deinitializes the Simple-TIMER peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void STIMER_DeInit(void)
{
    RCC_APBPeriphResetCmd(RCC_PRESETCTRL_STIMER, ENABLE);
    RCC_APBPeriphResetCmd(RCC_PRESETCTRL_STIMER, DISABLE);
}

/**
  * @brief  Initializes the Simple-TIMER according to 
  *   the specified parameters in the STIMER_InitTypeDef.
  * @param  STIMER_InitStruct: pointer to a TIMER_InitTypeDef
  *   structure that contains the configuration information for the specified TIMER peripheral.
  * @retval None
  */
void STIMER_Init(STIMER_InitTypeDef* STIMER_InitStruct)
{
    /* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STIMER_InitStruct->STIMER_Channel));
    assert_param(IS_STIMER_COUNT(STIMER_InitStruct->STIMER_Count));

    /* Load Count */
    if (STIMER_InitStruct->STIMER_Channel == STimer_Channel_0)
    {
        ST->LOADCOUNT0 = STIMER_InitStruct->STIMER_Count;
        ST->CONTROLREG0 |= ST_CTRL_MODE;
    }
    else
    {
        ST->LOADCOUNT1 = STIMER_InitStruct->STIMER_Count;
        ST->CONTROLREG1 |= ST_CTRL_MODE;
    }
}

/**
  * @brief  Enables or disables Simple-Timer.
  * @param  STimer_Channel: Simple Timer Channel.
  *   This parameter can be one of the following values:
  *     @arg STimer_Channel_0.
  *     @arg STimer_Channel_1.
  * @param  NewState: new state of the STIMER interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void STIMER_Cmd(uint32_t STimer_Channel, FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STimer_Channel));
	assert_param(IS_FUNCTIONAL_STATE(NewState));

	if (NewState != DISABLE)
    {
        if (STimer_Channel == STimer_Channel_0)
            ST->CONTROLREG0 |= ST_CTRL_ENABLE;
        else
            ST->CONTROLREG1 |= ST_CTRL_ENABLE;
    }
	else
    {
        if (STimer_Channel == STimer_Channel_0)
            ST->CONTROLREG0 &= ~ST_CTRL_ENABLE;
        else
            ST->CONTROLREG1 &= ~ST_CTRL_ENABLE;
    }
}

/**
  * @brief  Get Current Value.
  * @param  STimer_Channel: Simple Timer Channel.
  *   This parameter can be one of the following values:
  *     @arg STimer_Channel_0.
  *     @arg STimer_Channel_1.
  * @retval Current Value
  */
uint32_t STIMER_GetCurrentValue(uint32_t STimer_Channel)
{  
	/* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STimer_Channel));

    if (STimer_Channel == STimer_Channel_0)
        return ST->CURRENTVALUE0;
    else
        return ST->CURRENTVALUE1;
}

/**
  * @brief  Enables or disables STIMER interrupts.
  * @param  STimer_Channel: Simple Timer Channel.
  *   This parameter can be one of the following values:
  *     @arg STimer_Channel_0.
  *     @arg STimer_Channel_1.
  * @param  NewState: new state of the STIMER interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void STIMER_ITConfig(uint32_t STimer_Channel, FunctionalState NewState)
{
	/* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STimer_Channel));
	assert_param(IS_FUNCTIONAL_STATE(NewState));

	if (NewState != DISABLE)
	{
        if (STimer_Channel == STimer_Channel_0)
            ST->CONTROLREG0 &= ~ST_CTRL_INTR_MASK;
        else
            ST->CONTROLREG1 &= ~ST_CTRL_INTR_MASK;
	}
	else
	{
        if (STimer_Channel == STimer_Channel_0)
            ST->CONTROLREG0 |= ST_CTRL_INTR_MASK;
        else
            ST->CONTROLREG1 |= ST_CTRL_INTR_MASK;
	}
}

/**
  * @brief  Checks whether the STIMER interrupt has occurred or not.
  * @param  STimer_Channel: Simple Timer Channel.
  *   This parameter can be one of the following values:
  *     @arg STimer_Channel_0.
  *     @arg STimer_Channel_1.
  * @retval The new state(SET or RESET).
  */
ITStatus STIMER_GetITStatus(uint32_t STimer_Channel)
{
	ITStatus bitstatus = RESET, enablestatus = RESET;

	/* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STimer_Channel));

    if (STimer_Channel == STimer_Channel_0)
    {
        if (ST->CONTROLREG0 & ST_CTRL_INTR_MASK)
            enablestatus = RESET;
        else
            enablestatus = SET;
    }
    else
    {
        if (ST->CONTROLREG1 & ST_CTRL_INTR_MASK)
            enablestatus = RESET;
        else
            enablestatus = SET;
    }
    
    if (STimer_Channel == STimer_Channel_0)
    {
        if (ST->INTSTATUS0 && enablestatus)
            bitstatus = SET;
        else
            bitstatus = RESET;
    }
    else
    {
        if (ST->INTSTATUS1 && enablestatus)
            bitstatus = SET;
        else
            bitstatus = RESET;
    }

 	return bitstatus;
}

/**
  * @brief  Clears the STIMER's interrupt pending bits.
  * @param  STimer_Channel: Simple Timer Channel.
  *   This parameter can be one of the following values:
  *     @arg STimer_Channel_0.
  *     @arg STimer_Channel_1.
  * @retval None
  */
void STIMER_ClearITPendingBit(uint32_t STimer_Channel)
{
	/* Check the parameters */
    assert_param(IS_STIMER_CHANNEL_CONFIG(STimer_Channel));

	/* Clear the IT pending Bit */
    if (STimer_Channel == STimer_Channel_0)
    {
        ST->EOI0;
    }
    else
    {
        ST->EOI1;
    }
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT WeiLink Technology *****END OF FILE****/
